aDataSet = [];
deleteImg = '<img id="delete" src="../images/icons/cross.png" />';
updateImg = '<img id="update" src="../images/icons/edit.png" />';
action = "";
$(document).ready(function(){
	
	var txtIdReponse = $("#txtIdReponse");
	var txtContenuReponse = $("#txtContenuReponse");
	var sErrMsg = $("#sErrMsg");
	var allFields= $([]).add(txtIdReponse).add(txtContenuReponse).add(sErrMsg);
	
	var aCreateReponse =  $("#aCreateReponse");
	aCreateReponse.click(
		function() {
			openDialog("", "", "create");
        }
	);
	
	
	function openDialog(idReponse, contenuReponse, act){
		txtIdReponse.val( txtIdReponse.val() + idReponse );
		txtContenuReponse.val( txtContenuReponse.val() + contenuReponse );
		action = act;
		$('#dialog').dialog('open');
	}
	
	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 320,
		width:300,
		modal: true,
		buttons: {
			'Sauvegarder': function() {
				if(txtContenuReponse.val().length > 0){
					var reponse = {
						idReponse : txtIdReponse.val(),
						contenuReponse: txtContenuReponse.val(),
					};
					$.ajax({
						type : 'GET',
						url : '/qRH-WS/services/reponse/'+action,
						data : reponse,
						dataType : 'json',
						success : function(result, textStatus, jqXHR) {
							console.log('Success');
							location.reload();
						}
					});
					$(this).dialog('close');
				} else {
					sErrMsg.text("Veuillez saisir le contenu de la question");
				}
				
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	$.ajax({
		type : 'Get',
		url : '/qRH-WS/services/reponse/getAll',
		async: false,
		dataType : 'json',
		crossDomain : true,
		success : function(result, textStatus, jqXHR) {
			jQuery.each(result, function() {
				aDataSet.push(eval([this.idReponse, this.contenuReponse, updateImg, deleteImg]));		 
			});   
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(xhr.responseText);
		}
	});
	
	$('#tableContainer').html( '<table class="display" id="dataTableReponse"></table>' );
	
	$('#dataTableReponse').dataTable( {
		"aaData": aDataSet,
		"aoColumns": [
            { "sTitle": "Id" },
			{ "sTitle": "Contenu" },
			{ "sTitle": "Modifier" },
			{ "sTitle": "Supprimer" }
			],
		"iDisplayLength": aDataSet.length,
//XXX		Pour rendre la table soit selectionnable
//		"sDom": 'T<"clear">lfrtip',
//		"oTableTools": {
//			"sRowSelect": "multi",
//			"aButtons": [ "select_all", "select_none" ]
//		},
		"aLengthMenu": [[10, 25, 50, aDataSet.length], [10, 25, 50, "All"]]
	} );
	
	$('#dataTableReponse tbody tr').each(
		function() {
			
	        var nTds = $('td', this);
	        var idReponse = $(nTds[0]).text();
	        var contenuReponse = $(nTds[1]).text();
	        $('td', this).slice(-1).click(
        		function() {
		        	var answer = confirm("Voulez-vous vraiment supprimer cette reponse ?");
					if(answer) {
						$.ajax({
							type : 'Delete',
							url : '/qRH-WS/services/reponse/delete',
							async: false,
							data : idReponse,
							dataType : 'json',
							success : location.reload()
						});
		        	};
		        }
	        );
	        
	        $('td', this).slice(-2,-1).click(
        		function() {
        			openDialog(idReponse, contenuReponse, "update");
		        }
	        );
	    }
	);
	
	
});